<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
          <!-- 用一个行列布局 -->
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
            <el-col :span="20">
              <svg-icon icon-class="das" /><span>江苏传智播客教育科技股份有限公司</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd('')">一级部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <!-- 主体 -->
          <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick" default-expand-all>
            <template slot-scope="{ node, data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
            <el-col :span="20">
              <svg-icon icon-class="das" /><span>{{ node.label }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->       
                <el-col>{{ data.manager }}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hDel(data.id)"  v-if="data.children.length===0">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
            </template>
          </el-tree>
      </el-card>   
    </div>

  <el-dialog  :title="isEdit ? '编辑' : '添加'" :visible.sync="showDialog" :close-on-click-modal="false" :close-on-press-escape="false">
   <dept-dialog v-if="showDialog" :pid="curId"  @success="hSuccess" :is-edit="isEdit" @close="hClose" :originList="originList" />
  </el-dialog>
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/department'
import { ListToTree } from '@/utils/index'
import DeptDialog from './deptDialog.vue'
export default {
  data() {
    return {
     isEdit: false, // 是否处于编辑状态
     // 保存数据
     data: [],   
     // 设置具体的替换类名
     defaultProps: {
          children: 'children',
          label: 'name'
        },
     showDialog:false, // 是否显示弹层
     curId:'', // 当前选中的部门ID
     originList: []
    };
  },
  methods: {
     // 用户点击删除
      	async doDel(id) {
          try {
            const res = await delDepartment(id)
            console.log(res)
            // 1. 提示
            this.$message({ type: 'success', message: '删除成功!' })
            // 2. 更新数据
            this.loadDepartments()
          } catch (err) {
            console.log(err)
            this.$message({ type: 'error', message: '删除失败' })
          }
        },
        hDel(id) {
          this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // 做删除
            this.doDel(id)
          }).catch(() => {

          })
       },
     async loadDepartment() {
      const res = await getDepartment()
      console.log('res', res)
      // 去掉第一项
      res.data.depts.shift()
      // 用工具函数把 平铺的数组转成 树型数据
      this.data = ListToTree(res.data.depts)

      this.originList = res.data.depts.map(item => {
       return {
        id: item.id,
        pid: item.pid,
        name: item.name,
        code: item.code
       }

      })
      console.log('originList', this.originList)
    },
    handleNodeClick(data) {
      console.log(data);
    },
    // 用户点击了编辑
    hEdit(id) {
      // 1. 弹层出来
      this.showDialog = true
      // 2. 表单中要显示详情数据
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true
    },
    // 用户点击了新增
    hAdd(id) {
      // 显示弹框
      this.showDialog = true
      // 拿到当前的id, 方便传给子组件
      // console.log(data.id)
      // 更新当前操作的id
      this.curId = id
        // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = false
    },
    // 收到子组件中的close事件
      hClose() {
        this.showDialog = false
      },
    // 收到子组件中的success事件
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartment()
    }
  },
    created() {
      // 发请求
      this.loadDepartment()
  },
  components: { DeptDialog }
}
</script>